package com.wahoyu.exportwordtest;

import cn.hutool.core.date.DateTime;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;

public class JavaEChartsExample {
    public static void main(String[] args) {

        try {
            // 动态参数
            String outputPath = "/Users/wanghongyu/IdeaProjects/export-word-test/src/main/resources/echarts/" + new DateTime().toString("yyyy-MM-dd-hh:mm:ss") + ".png";

            // json压缩地址：https://www.json.cn/js-format/
            String jsonOption = "{title:{text:'StackedAreaChart'},tooltip:{trigger:'axis',axisPointer:{type:'cross',label:{backgroundColor:'#6a7985'}}},legend" + ":{data:['Email','UnionAds','VideoAds','Direct','SearchEngine']},toolbox:{feature:{saveAsImage:{}}},grid"+":{left:'3%',right:'4%',bottom:'3%',containLabel:true},xAxis:[{type:'category',boundaryGap:false,data:['Mon','Tue','Wed','Thu','Fri','Sat','Sun']}],yAxis:[{type:'value'}],series:[{name:'Email',type:'line',stack:'Total',areaStyle:{},emphasis:{focus:'series'},data:[120,132,101,134,90,230,210]},{name:'UnionAds',type:'line',stack:'Total',areaStyle:{},emphasis:{focus:'series'},data:[220,182,191,234,290,330,310]},{name:'VideoAds',type:'line',stack:'Total',areaStyle:{},emphasis:{focus:'series'},data:[150,232,201,154,190,330,410]},{name:'Direct',type:'line',stack:'Total',areaStyle:{},emphasis:{focus:'series'},data:[320,332,301,334,390,330,320]},{name:'SearchEngine',type:'line',stack:'Total',label:{show:true,position:'top'},areaStyle:{},emphasis:{focus:'series'},data:[820,932,901,934,1290,1330,1320]}]}";
            String jsPath = "/Users/wanghongyu/IdeaProjects/export-word-test/src/main/resources/echarts/generateECharts.js";
            String eChartPath = "/Users/wanghongyu/IdeaProjects/export-word-test/src/main/resources/echarts/echarts.min.js";

            // 构建命令
            ProcessBuilder pb = new ProcessBuilder("node", jsPath, outputPath, eChartPath);

            // 输出命令
            System.out.println("命令: " + pb.command());

            Process process = pb.start();

            // 发送JSON配置到标准输入
            try (OutputStream os = process.getOutputStream()) {
                os.write(jsonOption.getBytes(StandardCharsets.UTF_8));
                os.flush();
            }

            // 等待完成
            int exitCode = process.waitFor();
            System.out.println("退出码: " + exitCode);

            if (exitCode == 2) {
                // 读取错误
                try (BufferedReader reader = new BufferedReader(
                        new InputStreamReader(process.getErrorStream()))) {
                    String line;
                    while ((line = reader.readLine()) != null) {
                        System.err.println("[Node错误] " + line);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}